home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / Compilers⁄Interps / GCC-2.3.3r12 / Sources / local-alloc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-02-24  |  67.5 KB  |  2,151 lines  |  [TEXT/MPS ]

  1. /* Allocate registers within a basic block, for GNU compiler.
  2.    Copyright (C) 1987, 1988, 1991 Free Software Foundation, Inc.
  3.  
  4. This file is part of GNU CC.
  5.  
  6. GNU CC is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2, or (at your option)
  9. any later version.
  10.  
  11. GNU CC is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with GNU CC; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20.  
  21. /* Allocation of hard register numbers to pseudo registers is done in
  22.    two passes.  In this pass we consider only regs that are born and
  23.    die once within one basic block.  We do this one basic block at a
  24.    time.  Then the next pass allocates the registers that remain.
  25.    Two passes are used because this pass uses methods that work only
  26.    on linear code, but that do a better job than the general methods
  27.    used in global_alloc, and more quickly too.
  28.  
  29.    The assignments made are recorded in the vector reg_renumber
  30.    whose space is allocated here.  The rtl code itself is not altered.
  31.  
  32.    We assign each instruction in the basic block a number
  33.    which is its order from the beginning of the block.
  34.    Then we can represent the lifetime of a pseudo register with
  35.    a pair of numbers, and check for conflicts easily.
  36.    We can record the availability of hard registers with a
  37.    HARD_REG_SET for each instruction.  The HARD_REG_SET
  38.    contains 0 or 1 for each hard reg.
  39.  
  40.    To avoid register shuffling, we tie registers together when one
  41.    dies by being copied into another, or dies in an instruction that
  42.    does arithmetic to produce another.  The tied registers are
  43.    allocated as one.  Registers with different reg class preferences
  44.    can never be tied unless the class preferred by one is a subclass
  45.    of the one preferred by the other.
  46.  
  47.    Tying is represented with "quantity numbers".
  48.    A non-tied register is given a new quantity number.
  49.    Tied registers have the same quantity number.
  50.    
  51.    We have provision to exempt registers, even when they are contained
  52.    within the block, that can be tied to others that are not contained in it.
  53.    This is so that global_alloc could process them both and tie them then.
  54.    But this is currently disabled since tying in global_alloc is not
  55.    yet implemented.  */
  56.  
  57. #include <stdio.h>
  58. #include "config.h"
  59. #include "rtl.h"
  60. #include "flags.h"
  61. #include "basic-block.h"
  62. #include "regs.h"
  63. #include "hard-reg-set.h"
  64. #include "insn-config.h"
  65. #include "recog.h"
  66. #include "output.h"
  67. #ifdef MPW
  68. #include <Errors.h>
  69. #endif
  70.  
  71. /* Next quantity number available for allocation.  */
  72.  
  73. static int next_qty;
  74.  
  75. /* In all the following vectors indexed by quantity number.  */
  76.  
  77. /* Element Q is the hard reg number chosen for quantity Q,
  78.    or -1 if none was found.  */
  79.  
  80. static short *qty_phys_reg;
  81.  
  82. /* We maintain two hard register sets that indicate suggested hard registers
  83.    for each quantity.  The first, qty_phys_copy_sugg, contains hard registers
  84.    that are tied to the quantity by a simple copy.  The second contains all
  85.    hard registers that are tied to the quantity via an arithmetic operation.
  86.  
  87.    The former register set is given priority for allocation.  This tends to
  88.    eliminate copy insns.  */
  89.  
  90. /* Element Q is a set of hard registers that are suggested for quantity Q by
  91.    copy insns.  */
  92.  
  93. static HARD_REG_SET *qty_phys_copy_sugg;
  94.  
  95. /* Element Q is a set of hard registers that are suggested for quantity Q by
  96.    arithmetic insns.  */
  97.  
  98. static HARD_REG_SET *qty_phys_sugg;
  99.  
  100. /* Element Q is non-zero if there is a suggested register in
  101.    qty_phys_copy_sugg.  */
  102.  
  103. static char *qty_phys_has_copy_sugg;
  104.  
  105. /* Element Q is non-zero if there is a suggested register in qty_phys_sugg. */
  106.  
  107. static char *qty_phys_has_sugg;
  108.  
  109. /* Element Q is the number of refs to quantity Q.  */
  110.  
  111. static short *qty_n_refs;
  112.  
  113. /* Element Q is a reg class contained in (smaller than) the
  114.    preferred classes of all the pseudo regs that are tied in quantity Q.
  115.    This is the preferred class for allocating that quantity.  */
  116.  
  117. static enum reg_class *qty_min_class;
  118.  
  119. /* Insn number (counting from head of basic block)
  120.    where quantity Q was born.  -1 if birth has not been recorded.  */
  121.  
  122. static int *qty_birth;
  123.  
  124. /* Insn number (counting from head of basic block)
  125.    where quantity Q died.  Due to the way tying is done,
  126.    and the fact that we consider in this pass only regs that die but once,
  127.    a quantity can die only once.  Each quantity's life span
  128.    is a set of consecutive insns.  -1 if death has not been recorded.  */
  129.  
  130. static int *qty_death;
  131.  
  132. /* Number of words needed to hold the data in quantity Q.
  133.    This depends on its machine mode.  It is used for these purposes:
  134.    1. It is used in computing the relative importances of qtys,
  135.       which determines the order in which we look for regs for them.
  136.    2. It is used in rules that prevent tying several registers of
  137.       different sizes in a way that is geometrically impossible
  138.       (see combine_regs).  */
  139.  
  140. static int *qty_size;
  141.  
  142. /* This holds the mode of the registers that are tied to qty Q,
  143.    or VOIDmode if registers with differing modes are tied together.  */
  144.  
  145. static enum machine_mode *qty_mode;
  146.  
  147. /* Number of times a reg tied to qty Q lives across a CALL_INSN.  */
  148.  
  149. static int *qty_n_calls_crossed;
  150.  
  151. /* Register class within which we allocate qty Q if we can't get
  152.    its preferred class.  */
  153.  
  154. static enum reg_class *qty_alternate_class;
  155.  
  156. /* Element Q is the SCRATCH expression for which this quantity is being
  157.    allocated or 0 if this quantity is allocating registers.  */
  158.  
  159. static rtx *qty_scratch_rtx;
  160.  
  161. /* Element Q is the register number of one pseudo register whose
  162.    reg_qty value is Q, or -1 is this quantity is for a SCRATCH.  This
  163.    register should be the head of the chain maintained in reg_next_in_qty.  */
  164.  
  165. static short *qty_first_reg;
  166.  
  167. /* If (REG N) has been assigned a quantity number, is a register number
  168.    of another register assigned the same quantity number, or -1 for the
  169.    end of the chain.  qty_first_reg point to the head of this chain.  */
  170.  
  171. static short *reg_next_in_qty;
  172.  
  173. /* reg_qty[N] (where N is a pseudo reg number) is the qty number of that reg
  174.    if it is >= 0,
  175.    of -1 if this register cannot be allocated by local-alloc,
  176.    or -2 if not known yet.
  177.  
  178.    Note that if we see a use or death of pseudo register N with
  179.    reg_qty[N] == -2, register N must be local to the current block.  If
  180.    it were used in more than one block, we would have reg_qty[N] == -1.
  181.    This relies on the fact that if reg_basic_block[N] is >= 0, register N
  182.    will not appear in any other block.  We save a considerable number of
  183.    tests by exploiting this.
  184.  
  185.    If N is < FIRST_PSEUDO_REGISTER, reg_qty[N] is undefined and should not
  186.    be referenced.  */
  187.  
  188. static int *reg_qty;
  189.  
  190. /* The offset (in words) of register N within its quantity.
  191.    This can be nonzero if register N is SImode, and has been tied
  192.    to a subreg of a DImode register.  */
  193.  
  194. static char *reg_offset;
  195.  
  196. /* Vector of substitutions of register numbers,
  197.    used to map pseudo regs into hardware regs.
  198.    This is set up as a result of register allocation.
  199.    Element N is the hard reg assigned to pseudo reg N,
  200.    or is -1 if no hard reg was assigned.
  201.    If N is a hard reg number, element N is N.  */
  202.  
  203. short *reg_renumber;
  204.  
  205. /* Set of hard registers live at the current point in the scan
  206.    of the instructions in a basic block.  */
  207.  
  208. static HARD_REG_SET regs_live;
  209.  
  210. /* Each set of hard registers indicates registers live at a particular
  211.    point in the basic block.  For N even, regs_live_at[N] says which
  212.    hard registers are needed *after* insn N/2 (i.e., they may not
  213.    conflict with the outputs of insn N/2 or the inputs of insn N/2 + 1.
  214.  
  215.    If an object is to conflict with the inputs of insn J but not the
  216.    outputs of insn J + 1, we say it is born at index J*2 - 1.  Similarly,
  217.    if it is to conflict with the outputs of insn J but not the inputs of
  218.    insn J + 1, it is said to die at index J*2 + 1.  */
  219.  
  220. static HARD_REG_SET *regs_live_at;
  221.  
  222. /* Communicate local vars `insn_number' and `insn'
  223.    from `block_alloc' to `reg_is_set', `wipe_dead_reg', and `alloc_qty'.  */
  224. static int this_insn_number;
  225. static rtx this_insn;
  226.  
  227. static void block_alloc ();
  228. static void update_equiv_regs ();
  229. static int no_conflict_p ();
  230. static int combine_regs ();
  231. static void wipe_dead_reg ();
  232. static int find_free_reg ();
  233. static void reg_is_born ();
  234. static void reg_is_set ();
  235. static void mark_life ();
  236. static void post_mark_life ();
  237. static int qty_compare ();
  238. static int qty_compare_1 ();
  239. static int reg_meets_class_p ();
  240. static void update_qty_class ();
  241. static int requires_inout_p ();
  242.  
  243. /* Allocate a new quantity (new within current basic block)
  244.    for register number REGNO which is born at index BIRTH
  245.    within the block.  MODE and SIZE are info on reg REGNO.  */
  246.  
  247. static void
  248. alloc_qty (regno, mode, size, birth)
  249.      int regno;
  250.      enum machine_mode mode;
  251.      int size, birth;
  252. {
  253.   register int qty = next_qty++;
  254.  
  255.   reg_qty[regno] = qty;
  256.   reg_offset[regno] = 0;
  257.   reg_next_in_qty[regno] = -1;
  258.  
  259.   qty_first_reg[qty] = regno;
  260.   qty_size[qty] = size;
  261.   qty_mode[qty] = mode;
  262.   qty_birth[qty] = birth;
  263.   qty_n_calls_crossed[qty] = reg_n_calls_crossed[regno];
  264.   qty_min_class[qty] = reg_preferred_class (regno);
  265.   qty_alternate_class[qty] = reg_alternate_class (regno);
  266.   qty_n_refs[qty] = reg_n_refs[regno];
  267. }
  268.  
  269. /* Similar to `alloc_qty', but allocates a quantity for a SCRATCH rtx
  270.    used as operand N in INSN.  We assume here that the SCRATCH is used in
  271.    a CLOBBER.  */
  272.  
  273. static void
  274. alloc_qty_for_scratch (scratch, n, insn, insn_code_num, insn_number)
  275.      rtx scratch;
  276.      int n;
  277.      rtx insn;
  278.      int insn_code_num, insn_number;
  279. {
  280.   register int qty;
  281.   enum reg_class class;
  282.   char *p, c;
  283.   int i;
  284.  
  285. #ifdef REGISTER_CONSTRAINTS
  286.   /* If we haven't yet computed which alternative will be used, do so now.
  287.      Then set P to the constraints for that alternative.  */
  288.   if (which_alternative == -1)
  289.     if (! constrain_operands (insn_code_num, 0))
  290.       return;
  291.  
  292.   for (p = insn_operand_constraint[insn_code_num][n], i = 0;
  293.        *p && i < which_alternative; p++)
  294.     if (*p == ',')
  295.       i++;
  296.  
  297.   /* Compute the class required for this SCRATCH.  If we don't need a
  298.      register, the class will remain NO_REGS.  If we guessed the alternative
  299.      number incorrectly, reload will fix things up for us.  */
  300.  
  301.   class = NO_REGS;
  302.   while ((c = *p++) != '\0' && c != ',')
  303.     switch (c)
  304.       {
  305.       case '=':  case '+':  case '?':
  306.       case '#':  case '&':  case '!':
  307.       case '*':  case '%':  
  308.       case '0':  case '1':  case '2':  case '3':  case '4':
  309.       case 'm':  case '<':  case '>':  case 'V':  case 'o':
  310.       case 'E':  case 'F':  case 'G':  case 'H':
  311.       case 's':  case 'i':  case 'n':
  312.       case 'I':  case 'J':  case 'K':  case 'L':
  313.       case 'M':  case 'N':  case 'O':  case 'P':
  314. #ifdef EXTRA_CONSTRAINT
  315.       case 'Q':  case 'R':  case 'S':  case 'T':  case 'U':
  316. #endif
  317.       case 'p':
  318.     /* These don't say anything we care about.  */
  319.     break;
  320.  
  321.       case 'X':
  322.     /* We don't need to allocate this SCRATCH.  */
  323.     return;
  324.  
  325.       case 'g': case 'r':
  326.     class = reg_class_subunion[(int) class][(int) GENERAL_REGS];
  327.     break;
  328.  
  329.       default:
  330.     class
  331.       = reg_class_subunion[(int) class][(int) REG_CLASS_FROM_LETTER (c)];
  332.     break;
  333.       }
  334.  
  335.   /* If CLASS has only one register, don't allocate the SCRATCH here since
  336.      it will prevent that register from being used as a spill register.
  337.      reload will do the allocation.  */
  338.  
  339.   if (class == NO_REGS || reg_class_size[(int) class] == 1)
  340.     return;
  341.  
  342. #else /* REGISTER_CONSTRAINTS */
  343.  
  344.   class = GENERAL_REGS;
  345. #endif
  346.   
  347.  
  348.   qty = next_qty++;
  349.  
  350.   qty_first_reg[qty] = -1;
  351.   qty_scratch_rtx[qty] = scratch;
  352.   qty_size[qty] = GET_MODE_SIZE (GET_MODE (scratch));
  353.   qty_mode[qty] = GET_MODE (scratch);
  354.   qty_birth[qty] = 2 * insn_number - 1;
  355.   qty_death[qty] = 2 * insn_number + 1;
  356.   qty_n_calls_crossed[qty] = 0;
  357.   qty_min_class[qty] = class;
  358.   qty_alternate_class[qty] = NO_REGS;
  359.   qty_n_refs[qty] = 1;
  360. }
  361.  
  362. /* Main entry point of this file.  */
  363.  
  364. void
  365. local_alloc ()
  366. {
  367.   register int b, i;
  368.   int max_qty;
  369.  
  370.   /* Leaf functions and non-leaf functions have different needs.
  371.      If defined, let the machine say what kind of ordering we
  372.      should use.  */
  373. #ifdef ORDER_REGS_FOR_LOCAL_ALLOC
  374.   ORDER_REGS_FOR_LOCAL_ALLOC;
  375. #endif
  376.  
  377.   /* Promote REG_EQUAL notes to REG_EQUIV notes and adjust status of affected
  378.      registers.  */
  379.   update_equiv_regs ();
  380.  
  381.   /* This sets the maximum number of quantities we can have.  Quantity
  382.      numbers start at zero and we can have one for each pseudo plus the
  383.      number of SCRATCHes in the largest block, in the worst case.  */
  384.   max_qty = (max_regno - FIRST_PSEUDO_REGISTER) + max_scratch;
  385.  
  386.   /* Allocate vectors of temporary data.
  387.      See the declarations of these variables, above,
  388.      for what they mean.  */
  389.  
  390.   qty_phys_reg = (short *) alloca (max_qty * sizeof (short));
  391.   qty_phys_copy_sugg = (HARD_REG_SET *) alloca (max_qty * sizeof (HARD_REG_SET));
  392.   qty_phys_has_copy_sugg = (char *) alloca (max_qty * sizeof (char));
  393.   qty_phys_sugg = (HARD_REG_SET *) alloca (max_qty * sizeof (HARD_REG_SET));
  394.   qty_phys_has_sugg = (char *) alloca (max_qty * sizeof (char));
  395.   qty_birth = (int *) alloca (max_qty * sizeof (int));
  396.   qty_death = (int *) alloca (max_qty * sizeof (int));
  397.   qty_scratch_rtx = (rtx *) alloca (max_qty * sizeof (rtx));
  398.   qty_first_reg = (short *) alloca (max_qty * sizeof (short));
  399.   qty_size = (int *) alloca (max_qty * sizeof (int));
  400.   qty_mode = (enum machine_mode *) alloca (max_qty * sizeof (enum machine_mode));
  401.   qty_n_calls_crossed = (int *) alloca (max_qty * sizeof (int));
  402.   qty_min_class = (enum reg_class *) alloca (max_qty * sizeof (enum reg_class));
  403.   qty_alternate_class = (enum reg_class *) alloca (max_qty * sizeof (enum reg_class));
  404.   qty_n_refs = (short *) alloca (max_qty * sizeof (short));
  405.  
  406.   reg_qty = (int *) alloca (max_regno * sizeof (int));
  407.   reg_offset = (char *) alloca (max_regno * sizeof (char));
  408.   reg_next_in_qty = (short *) alloca (max_regno * sizeof (short));
  409.  
  410.   reg_renumber = (short *) oballoc (max_regno * sizeof (short));
  411.   for (i = 0; i < max_regno; i++)
  412.     reg_renumber[i] = -1;
  413.  
  414.   /* Determine which pseudo-registers can be allocated by local-alloc.
  415.      In general, these are the registers used only in a single block and
  416.      which only die once.  However, if a register's preferred class has only
  417.      one entry, don't allocate this register here unless it is preferred
  418.      or nothing since retry_global_alloc won't be able to move it to
  419.      GENERAL_REGS if a reload register of this class is needed.
  420.  
  421.      We need not be concerned with which block actually uses the register
  422.      since we will never see it outside that block.  */
  423.  
  424.   for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
  425.     {
  426.       if (reg_basic_block[i] >= 0 && reg_n_deaths[i] == 1
  427.       && (reg_alternate_class (i) == NO_REGS
  428.           || reg_class_size[(int) reg_preferred_class (i)] > 1))
  429.     reg_qty[i] = -2;
  430.       else
  431.     reg_qty[i] = -1;
  432.     }
  433.  
  434.   /* Force loop below to initialize entire quantity array.  */
  435.   next_qty = max_qty;
  436.  
  437.   /* Allocate each block's local registers, block by block.  */
  438.  
  439.   for (b = 0; b < n_basic_blocks; b++)
  440.     {
  441.       /* NEXT_QTY indicates which elements of the `qty_...'
  442.      vectors might need to be initialized because they were used
  443.      for the previous block; it is set to the entire array before
  444.      block 0.  Initialize those, with explicit loop if there are few,
  445.      else with bzero and bcopy.  Do not initialize vectors that are
  446.      explicit set by `alloc_qty'.  */
  447.  
  448.       if (next_qty < 6)
  449.     {
  450.       for (i = 0; i < next_qty; i++)
  451.         {
  452.           qty_scratch_rtx[i] = 0;
  453.           CLEAR_HARD_REG_SET (qty_phys_copy_sugg[i]);
  454.           qty_phys_has_copy_sugg[i] = 0;
  455.           CLEAR_HARD_REG_SET (qty_phys_sugg[i]);
  456.           qty_phys_has_sugg[i] = 0;
  457.         }
  458.     }
  459.       else
  460.     {
  461. #define CLEAR(vector)  \
  462.       bzero ((vector), (sizeof (*(vector))) * next_qty);
  463.  
  464.       CLEAR (qty_scratch_rtx);
  465.       CLEAR (qty_phys_copy_sugg);
  466.       CLEAR (qty_phys_has_copy_sugg);
  467.       CLEAR (qty_phys_sugg);
  468.       CLEAR (qty_phys_has_sugg);
  469.     }
  470.  
  471.       next_qty = 0;
  472.  
  473.       block_alloc (b);
  474. #ifdef USE_C_ALLOCA
  475.       alloca (0);
  476. #endif
  477.     }
  478. }
  479.  
  480. /* Depth of loops we are in while in update_equiv_regs.  */
  481. static int loop_depth;
  482.  
  483. /* Used for communication between the following two functions: contains
  484.    a MEM that we wish to ensure remains unchanged.  */
  485. static rtx equiv_mem;
  486.  
  487. /* Set nonzero if EQUIV_MEM is modified.  */
  488. static int equiv_mem_modified;
  489.  
  490. /* If EQUIV_MEM is modified by modifying DEST, indicate that it is modified.
  491.    Called via note_stores.  */
  492.  
  493. static void
  494. validate_equiv_mem_from_store (dest, set)
  495.      rtx dest;
  496.      rtx set;
  497. {
  498.   if ((GET_CODE (dest) == REG
  499.        && reg_overlap_mentioned_p (dest, equiv_mem))
  500.       || (GET_CODE (dest) == MEM
  501.       && true_dependence (dest, equiv_mem)))
  502.     equiv_mem_modified = 1;
  503. }
  504.  
  505. /* Verify that no store between START and the death of REG invalidates
  506.    MEMREF.  MEMREF is invalidated by modifying a register used in MEMREF,
  507.    by storing into an overlapping memory location, or with a non-const
  508.    CALL_INSN.
  509.  
  510.    Return 1 if MEMREF remains valid.  */
  511.  
  512. static int
  513. validate_equiv_mem (start, reg, memref)
  514.      rtx start;
  515.      rtx reg;
  516.      rtx memref;
  517. {
  518.   rtx insn;
  519.   rtx note;
  520.  
  521.   equiv_mem = memref;
  522.   equiv_mem_modified = 0;
  523.  
  524.   /* If the memory reference has side effects or is volatile, it isn't a
  525.      valid equivalence.  */
  526.   if (side_effects_p (memref))
  527.     return 0;
  528.  
  529.   for (insn = start; insn && ! equiv_mem_modified; insn = NEXT_INSN (insn))
  530.     {
  531.       if (GET_RTX_CLASS (GET_CODE (insn)) != 'i')
  532.     continue;
  533.  
  534.       if (find_reg_note (insn, REG_DEAD, reg))
  535.     return 1;
  536.  
  537.       if (GET_CODE (insn) == CALL_INSN && ! RTX_UNCHANGING_P (memref)
  538.       && ! CONST_CALL_P (insn))
  539.     return 0;
  540.  
  541.       note_stores (PATTERN (insn), validate_equiv_mem_from_store);
  542.  
  543.       /* If a register mentioned in MEMREF is modified via an
  544.      auto-increment, we lose the equivalence.  Do the same if one
  545.      dies; although we could extend the life, it doesn't seem worth
  546.      the trouble.  */
  547.  
  548.       for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
  549.     if ((REG_NOTE_KIND (note) == REG_INC
  550.          || REG_NOTE_KIND (note) == REG_DEAD)
  551.         && GET_CODE (XEXP (note, 0)) == REG
  552.         && reg_overlap_mentioned_p (XEXP (note, 0), memref))
  553.       return 0;
  554.     }
  555.  
  556.   return 0;
  557. }
  558.  
  559. /* TRUE if X references a memory location that would be affected by a store
  560.    to MEMREF.  */
  561.  
  562. static int
  563. memref_referenced_p (memref, x)
  564.      rtx x;
  565.      rtx memref;
  566. {
  567.   int i, j;
  568.   char *fmt;
  569.   enum rtx_code code = GET_CODE (x);
  570.  
  571.   switch (code)
  572.     {
  573.     case REG:
  574.     case CONST_INT:
  575.     case CONST:
  576.     case LABEL_REF:
  577.     case SYMBOL_REF:
  578.     case CONST_DOUBLE:
  579.     case PC:
  580.     case CC0:
  581.     case HIGH:
  582.     case LO_SUM:
  583.       return 0;
  584.  
  585.     case MEM:
  586.       if (true_dependence (memref, x))
  587.     return 1;
  588.       break;
  589.  
  590.     case SET:
  591.       /* If we are setting a MEM, it doesn't count (its address does), but any
  592.      other SET_DEST that has a MEM in it is referencing the MEM.  */
  593.       if (GET_CODE (SET_DEST (x)) == MEM)
  594.     {
  595.       if (memref_referenced_p (memref, XEXP (SET_DEST (x), 0)))
  596.         return 1;
  597.     }
  598.       else if (memref_referenced_p (memref, SET_DEST (x)))
  599.     return 1;
  600.  
  601.       return memref_referenced_p (memref, SET_SRC (x));
  602.     }
  603.  
  604.   fmt = GET_RTX_FORMAT (code);
  605.   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
  606.     switch (fmt[i])
  607.       {
  608.       case 'e':
  609.     if (memref_referenced_p (memref, XEXP (x, i)))
  610.       return 1;
  611.     break;
  612.       case 'E':
  613.     for (j = XVECLEN (x, i) - 1; j >= 0; j--)
  614.       if (memref_referenced_p (memref, XVECEXP (x, i, j)))
  615.         return 1;
  616.     break;
  617.       }
  618.  
  619.   return 0;
  620. }
  621.  
  622. /* TRUE if some insn in the range (START, END] references a memory location
  623.    that would be affected by a store to MEMREF.  */
  624.  
  625. static int
  626. memref_used_between_p (memref, start, end)
  627.      rtx memref;
  628.      rtx start;
  629.      rtx end;
  630. {
  631.   rtx insn;
  632.  
  633.   for (insn = NEXT_INSN (start); insn != NEXT_INSN (end);
  634.        insn = NEXT_INSN (insn))
  635.     if (GET_RTX_CLASS (GET_CODE (insn)) == 'i'
  636.     && memref_referenced_p (memref, PATTERN (insn)))
  637.       return 1;
  638.  
  639.   return 0;
  640. }
  641.  
  642. /* INSN is a copy from SRC to DEST, both registers, and SRC does not die
  643.    in INSN.
  644.  
  645.    Search forward to see if SRC dies before either it or DEST is modified,
  646.    but don't scan past the end of a basic block.  If so, we can replace SRC
  647.    with DEST and let SRC die in INSN. 
  648.  
  649.    This will reduce the number of registers live in that range and may enable
  650.    DEST to be tied to SRC, thus often saving one register in addition to a
  651.    register-register copy.  */
  652.  
  653. static void
  654. optimize_reg_copy_1 (insn, dest, src)
  655.      rtx insn;
  656.      rtx dest;
  657.      rtx src;
  658. {
  659.   rtx p, q;
  660.   rtx note;
  661.   rtx dest_death = 0;
  662.   int sregno = REGNO (src);
  663.   int dregno = REGNO (dest);
  664.  
  665.   if (sregno == dregno
  666. #ifdef SMALL_REGISTER_CLASSES
  667.       /* We don't want to mess with hard regs if register classes are small. */
  668.       || sregno < FIRST_PSEUDO_REGISTER || dregno < FIRST_PSEUDO_REGISTER
  669. #endif
  670.       /* We don't see all updates to SP if they are in an auto-inc memory
  671.      reference, so we must disallow this optimization on them.  */
  672.       || sregno == STACK_POINTER_REGNUM || dregno == STACK_POINTER_REGNUM)
  673.     return;
  674.  
  675.   for (p = NEXT_INSN (insn); p; p = NEXT_INSN (p))
  676.     {
  677.       if (GET_CODE (p) == CODE_LABEL || GET_CODE (p) == JUMP_INSN
  678.       || (GET_CODE (p) == NOTE
  679.           && (NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_BEG
  680.           || NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_END)))
  681.     break;
  682.  
  683.       if (GET_RTX_CLASS (GET_CODE (p)) != 'i')
  684.     continue;
  685.  
  686.       if (reg_set_p (src, p) || reg_set_p (dest, p)
  687.       /* Don't change a USE of a register.  */
  688.       || (GET_CODE (PATTERN (p)) == USE
  689.           && reg_overlap_mentioned_p (src, XEXP (PATTERN (p), 0))))
  690.     break;
  691.  
  692.       /* See if all of SRC dies in P.  This test is slightly more
  693.      conservative than it needs to be. */
  694.       if ((note = find_regno_note (p, REG_DEAD, sregno)) != 0
  695.       && GET_MODE (XEXP (note, 0)) == GET_MODE (src))
  696.     {
  697.       int failed = 0;
  698.       int length = 0;
  699.       int d_length = 0;
  700.       int n_calls = 0;
  701.       int d_n_calls = 0;
  702.  
  703.       /* If P is a CALL_INSN, SRC crosses one more call, since it
  704.          used to die there.  */
  705.  
  706.       if (GET_CODE (p) == CALL_INSN)
  707.         n_calls++;
  708.  
  709.       /* We can do the optimization.  Scan forward from INSN again,
  710.          replacing regs as we go.  Set FAILED if a replacement can't
  711.          be done.  In that case, we can't move the death note for SRC.
  712.          This should be rare.  */
  713.  
  714.       /* Set to stop at next insn.  */
  715.       for (q = next_real_insn (insn);
  716.            q != next_real_insn (p);
  717.            q = next_real_insn (q))
  718.         {
  719.           if (reg_overlap_mentioned_p (src, PATTERN (q)))
  720.         {
  721.           /* If SRC is a hard register, we might miss some
  722.              overlapping registers with validate_replace_rtx,
  723.              so we would have to undo it.  We can't if DEST is
  724.              present in the insn, so fail in that combination
  725.              of cases.  */
  726.           if (sregno < FIRST_PSEUDO_REGISTER
  727.               && reg_mentioned_p (dest, PATTERN (q)))
  728.             failed = 1;
  729.  
  730.           /* Replace all uses and make sure that the register
  731.              isn't still present.  */
  732.           else if (validate_replace_rtx (src, dest, q)
  733.                && (sregno >= FIRST_PSEUDO_REGISTER
  734.                    || ! reg_overlap_mentioned_p (src,
  735.                                  PATTERN (q))))
  736.             {
  737.               /* We assume that a register is used exactly once per
  738.              insn in the updates below.  If this is not correct,
  739.              no great harm is done.  */
  740.               if (sregno >= FIRST_PSEUDO_REGISTER)
  741.             reg_n_refs[sregno] -= loop_depth;
  742.               if (dregno >= FIRST_PSEUDO_REGISTER)
  743.             reg_n_refs[dregno] += loop_depth;
  744.             }
  745.           else
  746.             {
  747.               validate_replace_rtx (dest, src, q);
  748.               failed = 1;
  749.             }
  750.         }
  751.  
  752.           /* Count the insns and CALL_INSNs passed.  If we passed the
  753.          death note of DEST, show increased live length.  */
  754.           length++;
  755.           if (dest_death)
  756.         d_length++;
  757.  
  758.           if (GET_CODE (q) == CALL_INSN)
  759.         {
  760.           n_calls++;
  761.           if (dest_death)
  762.             d_n_calls++;
  763.         }
  764.  
  765.           /* If DEST dies here, remove the death note and save it for
  766.          later.  Make sure ALL of DEST dies here; again, this is
  767.          overly conservative.  */
  768.           if (dest_death == 0
  769.           && (dest_death = find_regno_note (q, REG_DEAD, dregno)) != 0
  770.           && GET_MODE (XEXP (dest_death, 0)) == GET_MODE (dest))
  771.         remove_note (q, dest_death);
  772.         }
  773.  
  774.       if (! failed)
  775.         {
  776.           if (sregno >= FIRST_PSEUDO_REGISTER)
  777.         {
  778.           reg_live_length[sregno] -= length;
  779.           reg_n_calls_crossed[sregno] -= n_calls;
  780.         }
  781.  
  782.           if (dregno >= FIRST_PSEUDO_REGISTER)
  783.         {
  784.           reg_live_length[dregno] += d_length;
  785.           reg_n_calls_crossed[dregno] += d_n_calls;
  786.         }
  787.  
  788.           /* Move death note of SRC from P to INSN.  */
  789.           remove_note (p, note);
  790.           XEXP (note, 1) = REG_NOTES (insn);
  791.           REG_NOTES (insn) = note;
  792.         }
  793.  
  794.       /* Put death note of DEST on P if we saw it die.  */
  795.       if (dest_death)
  796.         {
  797.           XEXP (dest_death, 1) = REG_NOTES (p);
  798.           REG_NOTES (p) = dest_death;
  799.         }
  800.  
  801.       return;
  802.     }
  803.  
  804.       /* If SRC is a hard register which is set or killed in some other
  805.      way, we can't do this optimization.  */
  806.       else if (sregno < FIRST_PSEUDO_REGISTER
  807.            && dead_or_set_p (p, src))
  808.     break;
  809.     }
  810. }
  811.  
  812. /* INSN is a copy of SRC to DEST, in which SRC dies.  See if we now have
  813.    a sequence of insns that modify DEST followed by an insn that sets
  814.    SRC to DEST in which DEST dies, with no prior modification of DEST.
  815.    (There is no need to check if the insns in between actually modify
  816.    DEST.  We should not have cases where DEST is not modified, but
  817.    the optimization is safe if no such modification is detected.)
  818.    In that case, we can replace all uses of DEST, starting with INSN and
  819.    ending with the set of SRC to DEST, with SRC.  We do not do this
  820.    optimization if a CALL_INSN is crossed unless SRC already crosses a
  821.    call.
  822.  
  823.    It is assumed that DEST and SRC are pseudos; it is too complicated to do
  824.    this for hard registers since the substitutions we may make might fail.  */
  825.  
  826. static void
  827. optimize_reg_copy_2 (insn, dest, src)
  828.      rtx insn;
  829.      rtx dest;
  830.      rtx src;
  831. {
  832.   rtx p, q;
  833.   rtx set;
  834.   int sregno = REGNO (src);
  835.   int dregno = REGNO (dest);
  836.  
  837.   for (p = NEXT_INSN (insn); p; p = NEXT_INSN (p))
  838.     {
  839.       if (GET_CODE (p) == CODE_LABEL || GET_CODE (p) == JUMP_INSN
  840.       || (GET_CODE (p) == NOTE
  841.           && (NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_BEG
  842.           || NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_END)))
  843.     break;
  844.  
  845.       if (GET_RTX_CLASS (GET_CODE (p)) != 'i')
  846.     continue;
  847.  
  848.       set = single_set (p);
  849.       if (set && SET_SRC (set) == dest && SET_DEST (set) == src
  850.       && find_reg_note (p, REG_DEAD, dest))
  851.     {
  852.       /* We can do the optimization.  Scan forward from INSN again,
  853.          replacing regs as we go.  */
  854.  
  855.       /* Set to stop at next insn.  */
  856.       for (q = insn; q != NEXT_INSN (p); q = NEXT_INSN (q))
  857.         if (GET_RTX_CLASS (GET_CODE (q)) == 'i')
  858.           {
  859.         if (reg_mentioned_p (dest, PATTERN (q)))
  860.           {
  861.             PATTERN (q) = replace_rtx (PATTERN (q), dest, src);
  862.  
  863.             /* We assume that a register is used exactly once per
  864.                insn in the updates below.  If this is not correct,
  865.                no great harm is done.  */
  866.             reg_n_refs[dregno] -= loop_depth;
  867.             reg_n_refs[sregno] += loop_depth;
  868.           }
  869.  
  870.  
  871.           if (GET_CODE (q) == CALL_INSN)
  872.         {
  873.           reg_n_calls_crossed[dregno]--;
  874.           reg_n_calls_crossed[sregno]++;
  875.         }
  876.           }
  877.  
  878.       remove_note (p, find_reg_note (p, REG_DEAD, dest));
  879.       reg_n_deaths[dregno]--;
  880.       remove_note (insn, find_reg_note (insn, REG_DEAD, src));
  881.       reg_n_deaths[sregno]--;
  882.       return;
  883.     }
  884.  
  885.       if (reg_set_p (src, p)
  886.       || (GET_CODE (p) == CALL_INSN && reg_n_calls_crossed[sregno] == 0))
  887.     break;
  888.     }
  889. }
  890.           
  891. /* Find registers that are equivalent to a single value throughout the
  892.    compilation (either because they can be referenced in memory or are set once
  893.    from a single constant).  Lower their priority for a register.
  894.  
  895.    If such a register is only referenced once, try substituting its value
  896.    into the using insn.  If it succeeds, we can eliminate the register
  897.    completely.  */
  898.  
  899. static void
  900. update_equiv_regs ()
  901. {
  902.   rtx *reg_equiv_init_insn = (rtx *) alloca (max_regno * sizeof (rtx *));
  903.   rtx *reg_equiv_replacement = (rtx *) alloca (max_regno * sizeof (rtx *));
  904.   rtx insn;
  905.  
  906.   bzero (reg_equiv_init_insn, max_regno * sizeof (rtx *));
  907.   bzero (reg_equiv_replacement, max_regno * sizeof (rtx *));
  908.  
  909.   init_alias_analysis ();
  910.  
  911.   loop_depth = 1;
  912.  
  913.   /* Scan the insns and find which registers have equivalences.  Do this
  914.      in a separate scan of the insns because (due to -fcse-follow-jumps)
  915.      a register can be set below its use.  */
  916.   for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
  917.     {
  918.       rtx note;
  919.       rtx set = single_set (insn);
  920.       rtx dest;
  921.       int regno;
  922.  
  923.       if (GET_CODE (insn) == NOTE)
  924.     {
  925.       if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_BEG)
  926.         loop_depth++;
  927.       else if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_END)
  928.         loop_depth--;
  929.     }
  930.  
  931.       /* If this insn contains more (or less) than a single SET, ignore it.  */
  932.       if (set == 0)
  933.     continue;
  934.  
  935.       dest = SET_DEST (set);
  936.  
  937.       /* If this sets a MEM to the contents of a REG that is only used
  938.      in a single basic block, see if the register is always equivalent
  939.      to that memory location and if moving the store from INSN to the
  940.      insn that set REG is safe.  If so, put a REG_EQUIV note on the
  941.      initializing insn.  */
  942.  
  943.       if (GET_CODE (dest) == MEM && GET_CODE (SET_SRC (set)) == REG
  944.       && (regno = REGNO (SET_SRC (set))) >= FIRST_PSEUDO_REGISTER
  945.       && reg_basic_block[regno] >= 0
  946.       && reg_equiv_init_insn[regno] != 0
  947.       && validate_equiv_mem (reg_equiv_init_insn[regno], SET_SRC (set),
  948.                  dest)
  949.       && ! memref_used_between_p (SET_DEST (set),
  950.                       reg_equiv_init_insn[regno], insn))
  951.     REG_NOTES (reg_equiv_init_insn[regno])
  952.       = gen_rtx (EXPR_LIST, REG_EQUIV, dest,
  953.              REG_NOTES (reg_equiv_init_insn[regno]));
  954.  
  955.       /* If this is a register-register copy where SRC is not dead, see if we
  956.      can optimize it.  */
  957.       if (flag_expensive_optimizations && GET_CODE (dest) == REG
  958.       && GET_CODE (SET_SRC (set)) == REG
  959.       && ! find_reg_note (insn, REG_DEAD, SET_SRC (set)))
  960.     optimize_reg_copy_1 (insn, dest, SET_SRC (set));
  961.  
  962.       /* Similarly for a pseudo-pseudo copy when SRC is dead.  */
  963.       else if (flag_expensive_optimizations && GET_CODE (dest) == REG
  964.            && REGNO (dest) >= FIRST_PSEUDO_REGISTER
  965.            && GET_CODE (SET_SRC (set)) == REG
  966.            && REGNO (SET_SRC (set)) >= FIRST_PSEUDO_REGISTER
  967.            && find_reg_note (insn, REG_DEAD, SET_SRC (set)))
  968.     optimize_reg_copy_2 (insn, dest, SET_SRC (set));
  969.  
  970.       /* Otherwise, we only handle the case of a pseudo register being set
  971.      once.  */
  972.       if (GET_CODE (dest) != REG
  973.       || (regno = REGNO (dest)) < FIRST_PSEUDO_REGISTER
  974.       || reg_n_sets[regno] != 1)
  975.     continue;
  976.  
  977.       note = find_reg_note (insn, REG_EQUAL, NULL_RTX);
  978.  
  979.       /* Record this insn as initializing this register.  */
  980.       reg_equiv_init_insn[regno] = insn;
  981.  
  982.       /* If this register is known to be equal to a constant, record that
  983.      it is always equivalent to the constant.  */
  984.       if (note && CONSTANT_P (XEXP (note, 0)))
  985.     PUT_MODE (note, (enum machine_mode) REG_EQUIV);
  986.  
  987.       /* If this insn introduces a "constant" register, decrease the priority
  988.      of that register.  Record this insn if the register is only used once
  989.      more and the equivalence value is the same as our source.
  990.  
  991.      The latter condition is checked for two reasons:  First, it is an
  992.      indication that it may be more efficient to actually emit the insn
  993.      as written (if no registers are available, reload will substitute
  994.      the equivalence).  Secondly, it avoids problems with any registers
  995.      dying in this insn whose death notes would be missed.
  996.  
  997.      If we don't have a REG_EQUIV note, see if this insn is loading
  998.      a register used only in one basic block from a MEM.  If so, and the
  999.      MEM remains unchanged for the life of the register, add a REG_EQUIV
  1000.      note.  */
  1001.      
  1002.       note = find_reg_note (insn, REG_EQUIV, NULL_RTX);
  1003.  
  1004.       if (note == 0 && reg_basic_block[regno] >= 0
  1005.       && GET_CODE (SET_SRC (set)) == MEM
  1006.       && validate_equiv_mem (insn, dest, SET_SRC (set)))
  1007.     REG_NOTES (insn) = note = gen_rtx (EXPR_LIST, REG_EQUIV, SET_SRC (set),
  1008.                        REG_NOTES (insn));
  1009.  
  1010.       /* Don't mess with things live during setjmp.  */
  1011.       if (note && reg_live_length[regno] >= 0)
  1012.     {
  1013.       int regno = REGNO (dest);
  1014.  
  1015.       /* Note that the statement below does not affect the priority
  1016.          in local-alloc!  */
  1017.       reg_live_length[regno] *= 2;
  1018.  
  1019.       /* If the register is referenced exactly twice, meaning it is set
  1020.          once and used once, indicate that the reference may be replaced
  1021.          by the equivalence we computed above.  If the register is only
  1022.          used in one basic block, this can't succeed or combine would
  1023.          have done it.
  1024.  
  1025.          It would be nice to use "loop_depth * 2" in the compare
  1026.          below.  Unfortunately, LOOP_DEPTH need not be constant within
  1027.          a basic block so this would be too complicated.
  1028.  
  1029.          This case normally occurs when a parameter is read from memory
  1030.          and then used exactly once, not in a loop.  */
  1031.  
  1032.       if (reg_n_refs[regno] == 2
  1033.           && reg_basic_block[regno] < 0
  1034.           && rtx_equal_p (XEXP (note, 0), SET_SRC (set)))
  1035.         reg_equiv_replacement[regno] = SET_SRC (set);
  1036.     }
  1037.     }
  1038.  
  1039.   /* Now scan all regs killed in an insn to see if any of them are registers
  1040.      only used that once.  If so, see if we can replace the reference with
  1041.      the equivalent from.  If we can, delete the initializing reference
  1042.      and this register will go away.  */
  1043.   for (insn = next_active_insn (get_insns ());
  1044.        insn;
  1045.        insn = next_active_insn (insn))
  1046.     {
  1047.       rtx link;
  1048.  
  1049.       for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
  1050.     if (REG_NOTE_KIND (link) == REG_DEAD
  1051.         /* Make sure this insn still refers to the register.  */
  1052.         && reg_mentioned_p (XEXP (link, 0), PATTERN (insn)))
  1053.       {
  1054.         int regno = REGNO (XEXP (link, 0));
  1055.  
  1056.         if (reg_equiv_replacement[regno]
  1057.         && validate_replace_rtx (regno_reg_rtx[regno],
  1058.                      reg_equiv_replacement[regno], insn))
  1059.           {
  1060.         rtx equiv_insn = reg_equiv_init_insn[regno];
  1061.  
  1062.         remove_death (regno, insn);
  1063.         reg_n_refs[regno] = 0;
  1064.         PUT_CODE (equiv_insn, NOTE);
  1065.         NOTE_LINE_NUMBER (equiv_insn) = NOTE_INSN_DELETED;
  1066.         NOTE_SOURCE_FILE (equiv_insn) = 0;
  1067.           }
  1068.       }
  1069.     }
  1070. }
  1071.  
  1072. /* Allocate hard regs to the pseudo regs used only within block number B.
  1073.    Only the pseudos that die but once can be handled.  */
  1074.  
  1075. static void
  1076. block_alloc (b)
  1077.      int b;
  1078. {
  1079.   register int i, q;
  1080.   register rtx insn;
  1081.   rtx note;
  1082.   int insn_number = 0;
  1083.   int insn_count = 0;
  1084.   int max_uid = get_max_uid ();
  1085.   short *qty_order;
  1086.   int no_conflict_combined_regno = -1;
  1087.  
  1088.   /* Count the instructions in the basic block.  */
  1089.  
  1090.   insn = basic_block_end[b];
  1091.   while (1)
  1092.     {
  1093.       if (GET_CODE (insn) != NOTE)
  1094.     if (++insn_count > max_uid)
  1095.       abort ();
  1096.       if (insn == basic_block_head[b])
  1097.     break;
  1098.       insn = PREV_INSN (insn);
  1099.     }
  1100.  
  1101.   /* +2 to leave room for a post_mark_life at the last insn and for
  1102.      the birth of a CLOBBER in the first insn.  */
  1103.   regs_live_at = (HARD_REG_SET *) alloca ((2 * insn_count + 2)
  1104.                       * sizeof (HARD_REG_SET));
  1105.   bzero (regs_live_at, (2 * insn_count + 2) * sizeof (HARD_REG_SET));
  1106.  
  1107.   /* Initialize table of hardware registers currently live.  */
  1108.  
  1109. #ifdef HARD_REG_SET
  1110.   regs_live = *basic_block_live_at_start[b];
  1111. #else
  1112.   COPY_HARD_REG_SET (regs_live, basic_block_live_at_start[b]);
  1113. #endif
  1114.  
  1115.   /* This loop scans the instructions of the basic block
  1116.      and assigns quantities to registers.
  1117.      It computes which registers to tie.  */
  1118.  
  1119.   insn = basic_block_head[b];
  1120.   while (1)
  1121.     {
  1122.       register rtx body = PATTERN (insn);
  1123.  
  1124.       if (GET_CODE (insn) != NOTE)
  1125.     insn_number++;
  1126.  
  1127.       if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
  1128.     {
  1129.       register rtx link, set;
  1130.       register int win = 0;
  1131.       register rtx r0, r1;
  1132.       int combined_regno = -1;
  1133.       int i;
  1134.       int insn_code_number = recog_memoized (insn);
  1135.  
  1136.       this_insn_number = insn_number;
  1137.       this_insn = insn;
  1138.  
  1139.       if (insn_code_number >= 0)
  1140.         insn_extract (insn);
  1141.       which_alternative = -1;
  1142.  
  1143.       /* Is this insn suitable for tying two registers?
  1144.          If so, try doing that.
  1145.          Suitable insns are those with at least two operands and where
  1146.          operand 0 is an output that is a register that is not
  1147.          earlyclobber.
  1148.          For a commutative operation, try (set reg0 (arithop ... reg1)).
  1149.          Subregs in place of regs are also ok.
  1150.  
  1151.          If tying is done, WIN is set nonzero.  */
  1152.  
  1153.       if (insn_code_number >= 0
  1154. #ifdef REGISTER_CONSTRAINTS
  1155.           && insn_n_operands[insn_code_number] > 1
  1156.           && insn_operand_constraint[insn_code_number][0][0] == '='
  1157.           && insn_operand_constraint[insn_code_number][0][1] != '&'
  1158. #else
  1159.           && GET_CODE (PATTERN (insn)) == SET
  1160.           && rtx_equal_p (SET_DEST (PATTERN (insn)), recog_operand[0])
  1161. #endif
  1162.           )
  1163.         {
  1164.           r0 = recog_operand[0];
  1165.           r1 = recog_operand[1];
  1166.  
  1167.           /* If the first operand is an address, find a register in it.
  1168.          There may be more than one register, but we only try one of
  1169.          them.  */
  1170.           if (
  1171. #ifdef REGISTER_CONSTRAINTS
  1172.           insn_operand_constraint[insn_code_number][1][0] == 'p'
  1173. #else
  1174.           insn_operand_address_p[insn_code_number][1]
  1175. #endif
  1176.           )
  1177.         while (GET_CODE (r1) == PLUS || GET_CODE (r1) == MULT)
  1178.           r1 = XEXP (r1, 0);
  1179.  
  1180.           if (GET_CODE (r0) == REG || GET_CODE (r0) == SUBREG)
  1181.         {
  1182.           /* We have two priorities for hard register preferences.
  1183.              If we have a move insn or an insn whose first input can
  1184.              only be in the same register as the output, give
  1185.              priority to an equivalence found from that insn.  */
  1186. #ifdef REGISTER_CONSTRAINTS
  1187.           int may_save_copy
  1188.             = ((SET_DEST (body) == r0 && SET_SRC (body) == r1)
  1189.                || (r1 == recog_operand[1]
  1190.                && (requires_inout_p (insn_operand_constraint[insn_code_number][1]))));
  1191. #else
  1192.           int may_save_copy = 0;
  1193. #endif
  1194.  
  1195.           if (GET_CODE (r1) == REG || GET_CODE (r1) == SUBREG)
  1196.             win = combine_regs (r1, r0, may_save_copy,
  1197.                     insn_number, insn, 0);
  1198.  
  1199.           if (win == 0
  1200.               && insn_n_operands[insn_code_number] > 2
  1201. #ifdef REGISTER_CONSTRAINTS
  1202.               && insn_operand_constraint[insn_code_number][1][0] == '%'
  1203. #else
  1204.               && GET_CODE (PATTERN (insn)) == SET
  1205.               && (GET_RTX_CLASS (GET_CODE (SET_SRC (PATTERN (insn))))
  1206.               == 'c')
  1207.               && rtx_equal_p (recog_operand[2],
  1208.                       XEXP (SET_SRC (PATTERN (insn)), 0))
  1209. #endif
  1210.               && (r1 = recog_operand[2],
  1211.               GET_CODE (r1) == REG || GET_CODE (r1) == SUBREG))
  1212.             win = combine_regs (r1, r0, may_save_copy,
  1213.                     insn_number, insn, 0);
  1214.         }
  1215.         }
  1216.  
  1217.       /* Recognize an insn sequence with an ultimate result
  1218.          which can safely overlap one of the inputs.
  1219.          The sequence begins with a CLOBBER of its result,
  1220.          and ends with an insn that copies the result to itself
  1221.          and has a REG_EQUAL note for an equivalent formula.
  1222.          That note indicates what the inputs are.
  1223.          The result and the input can overlap if each insn in
  1224.          the sequence either doesn't mention the input
  1225.          or has a REG_NO_CONFLICT note to inhibit the conflict.
  1226.  
  1227.          We do the combining test at the CLOBBER so that the
  1228.          destination register won't have had a quantity number
  1229.          assigned, since that would prevent combining.  */
  1230.  
  1231.       if (GET_CODE (PATTERN (insn)) == CLOBBER
  1232.           && (r0 = XEXP (PATTERN (insn), 0),
  1233.           GET_CODE (r0) == REG)
  1234.           && (link = find_reg_note (insn, REG_LIBCALL, NULL_RTX)) != 0
  1235.           && GET_CODE (XEXP (link, 0)) == INSN
  1236.           && (set = single_set (XEXP (link, 0))) != 0
  1237.           && SET_DEST (set) == r0 && SET_SRC (set) == r0
  1238.           && (note = find_reg_note (XEXP (link, 0), REG_EQUAL,
  1239.                     NULL_RTX)) != 0)
  1240.         {
  1241.           if (r1 = XEXP (note, 0), GET_CODE (r1) == REG
  1242.           /* Check that we have such a sequence.  */
  1243.           && no_conflict_p (insn, r0, r1))
  1244.         win = combine_regs (r1, r0, 1, insn_number, insn, 1);
  1245.           else if (GET_RTX_FORMAT (GET_CODE (XEXP (note, 0)))[0] == 'e'
  1246.                && (r1 = XEXP (XEXP (note, 0), 0),
  1247.                GET_CODE (r1) == REG || GET_CODE (r1) == SUBREG)
  1248.                && no_conflict_p (insn, r0, r1))
  1249.         win = combine_regs (r1, r0, 0, insn_number, insn, 1);
  1250.  
  1251.           /* Here we care if the operation to be computed is
  1252.          commutative.  */
  1253.           else if ((GET_CODE (XEXP (note, 0)) == EQ
  1254.             || GET_CODE (XEXP (note, 0)) == NE
  1255.             || GET_RTX_CLASS (GET_CODE (XEXP (note, 0))) == 'c')
  1256.                && (r1 = XEXP (XEXP (note, 0), 1),
  1257.                (GET_CODE (r1) == REG || GET_CODE (r1) == SUBREG))
  1258.                && no_conflict_p (insn, r0, r1))
  1259.         win = combine_regs (r1, r0, 0, insn_number, insn, 1);
  1260.  
  1261.           /* If we did combine something, show the register number
  1262.          in question so that we know to ignore its death.  */
  1263.           if (win)
  1264.         no_conflict_combined_regno = REGNO (r1);
  1265.         }
  1266.  
  1267.       /* If registers were just tied, set COMBINED_REGNO
  1268.          to the number of the register used in this insn
  1269.          that was tied to the register set in this insn.
  1270.          This register's qty should not be "killed".  */
  1271.  
  1272.       if (win)
  1273.         {
  1274.           while (GET_CODE (r1) == SUBREG)
  1275.         r1 = SUBREG_REG (r1);
  1276.           combined_regno = REGNO (r1);
  1277.         }
  1278.  
  1279.       /* Mark the death of everything that dies in this instruction,
  1280.          except for anything that was just combined.  */
  1281.  
  1282.       for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
  1283.         if (REG_NOTE_KIND (link) == REG_DEAD
  1284.         && GET_CODE (XEXP (link, 0)) == REG
  1285.         && combined_regno != REGNO (XEXP (link, 0))
  1286.         && (no_conflict_combined_regno != REGNO (XEXP (link, 0))
  1287.             || ! find_reg_note (insn, REG_NO_CONFLICT, XEXP (link, 0))))
  1288.           wipe_dead_reg (XEXP (link, 0), 0);
  1289.  
  1290.       /* Allocate qty numbers for all registers local to this block
  1291.          that are born (set) in this instruction.
  1292.          A pseudo that already has a qty is not changed.  */
  1293.  
  1294.       note_stores (PATTERN (insn), reg_is_set);
  1295.  
  1296.       /* If anything is set in this insn and then unused, mark it as dying
  1297.          after this insn, so it will conflict with our outputs.  This
  1298.          can't match with something that combined, and it doesn't matter
  1299.          if it did.  Do this after the calls to reg_is_set since these
  1300.          die after, not during, the current insn.  */
  1301.  
  1302.       for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
  1303.         if (REG_NOTE_KIND (link) == REG_UNUSED
  1304.         && GET_CODE (XEXP (link, 0)) == REG)
  1305.           wipe_dead_reg (XEXP (link, 0), 1);
  1306.  
  1307. #ifndef SMALL_REGISTER_CLASSES
  1308.       /* Allocate quantities for any SCRATCH operands of this insn.  We
  1309.          don't do this for machines with small register classes because
  1310.          those machines can use registers explicitly mentioned in the
  1311.          RTL as spill registers and our usage of hard registers
  1312.          explicitly for SCRATCH operands will conflict.  On those machines,
  1313.          reload will allocate the SCRATCH.  */
  1314.  
  1315.       if (insn_code_number >= 0)
  1316.         for (i = 0; i < insn_n_operands[insn_code_number]; i++)
  1317.           if (GET_CODE (recog_operand[i]) == SCRATCH)
  1318.         alloc_qty_for_scratch (recog_operand[i], i, insn,
  1319.                        insn_code_number, insn_number);
  1320. #endif
  1321.  
  1322.       /* If this is an insn that has a REG_RETVAL note pointing at a 
  1323.          CLOBBER insn, we have reached the end of a REG_NO_CONFLICT
  1324.          block, so clear any register number that combined within it.  */
  1325.       if ((note = find_reg_note (insn, REG_RETVAL, NULL_RTX)) != 0
  1326.           && GET_CODE (XEXP (note, 0)) == INSN
  1327.           && GET_CODE (PATTERN (XEXP (note, 0))) == CLOBBER)
  1328.         no_conflict_combined_regno = -1;
  1329.     }
  1330.  
  1331.       /* Set the registers live after INSN_NUMBER.  Note that we never
  1332.      record the registers live before the block's first insn, since no
  1333.      pseudos we care about are live before that insn.  */
  1334.  
  1335.       IOR_HARD_REG_SET (regs_live_at[2 * insn_number], regs_live);
  1336.       IOR_HARD_REG_SET (regs_live_at[2 * insn_number + 1], regs_live);
  1337.  
  1338.       if (insn == basic_block_end[b])
  1339.     break;
  1340.  
  1341.       insn = NEXT_INSN (insn);
  1342.     }
  1343.  
  1344.   /* Now every register that is local to this basic block
  1345.      should have been given a quantity, or else -1 meaning ignore it.
  1346.      Every quantity should have a known birth and death.  
  1347.  
  1348.      Order the qtys so we assign them registers in order of 
  1349.      decreasing length of life.  Normally call qsort, but if we 
  1350.      have only a very small number of quantities, sort them ourselves.  */
  1351.  
  1352.   qty_order = (short *) alloca (next_qty * sizeof (short));
  1353.   for (i = 0; i < next_qty; i++)
  1354.     qty_order[i] = i;
  1355.  
  1356. #define EXCHANGE(I1, I2)  \
  1357.   { i = qty_order[I1]; qty_order[I1] = qty_order[I2]; qty_order[I2] = i; }
  1358.  
  1359.   switch (next_qty)
  1360.     {
  1361.     case 3:
  1362.       /* Make qty_order[2] be the one to allocate last.  */
  1363.       if (qty_compare (0, 1) > 0)
  1364.     EXCHANGE (0, 1);
  1365.       if (qty_compare (1, 2) > 0)
  1366.     EXCHANGE (2, 1);
  1367.  
  1368.       /* ... Fall through ... */
  1369.     case 2:
  1370.       /* Put the best one to allocate in qty_order[0].  */
  1371.       if (qty_compare (0, 1) > 0)
  1372.     EXCHANGE (0, 1);
  1373.  
  1374.       /* ... Fall through ... */
  1375.  
  1376.     case 1:
  1377.     case 0:
  1378.       /* Nothing to do here.  */
  1379.       break;
  1380.  
  1381.     default:
  1382.       qsort (qty_order, next_qty, sizeof (short), qty_compare_1);
  1383.     }
  1384.  
  1385.   /* Try to put each quantity in a suggested physical register, if it has one.
  1386.      This may cause registers to be allocated that otherwise wouldn't be, but
  1387.      this seems acceptable in local allocation (unlike global allocation).  */
  1388.   for (i = 0; i < next_qty; i++)
  1389.     {
  1390.       q = qty_order[i];
  1391.       if (qty_phys_has_sugg[q] || qty_phys_has_copy_sugg[q])
  1392.     qty_phys_reg[q] = find_free_reg (qty_min_class[q], qty_mode[q], q,
  1393.                      0, 1, qty_birth[q], qty_death[q]);
  1394.       else
  1395.     qty_phys_reg[q] = -1;
  1396.     }
  1397.  
  1398.   /* Now for each qty that is not a hardware register,
  1399.      look for a hardware register to put it in.
  1400.      First try the register class that is cheapest for this qty,
  1401.      if there is more than one class.  */
  1402.  
  1403.   for (i = 0; i < next_qty; i++)
  1404.     {
  1405.       q = qty_order[i];
  1406.       if (qty_phys_reg[q] < 0)
  1407.     {
  1408.       if (N_REG_CLASSES > 1)
  1409.         {
  1410.           qty_phys_reg[q] = find_free_reg (qty_min_class[q], 
  1411.                            qty_mode[q], q, 0, 0,
  1412.                            qty_birth[q], qty_death[q]);
  1413.           if (qty_phys_reg[q] >= 0)
  1414.         continue;
  1415.         }
  1416.  
  1417.       if (qty_alternate_class[q] != NO_REGS)
  1418.         qty_phys_reg[q] = find_free_reg (qty_alternate_class[q],
  1419.                          qty_mode[q], q, 0, 0,
  1420.                          qty_birth[q], qty_death[q]);
  1421.     }
  1422.     }
  1423.  
  1424.   /* Now propagate the register assignments
  1425.      to the pseudo regs belonging to the qtys.  */
  1426.  
  1427.   for (q = 0; q < next_qty; q++)
  1428.     if (qty_phys_reg[q] >= 0)
  1429.       {
  1430.     for (i = qty_first_reg[q]; i >= 0; i = reg_next_in_qty[i])
  1431.       reg_renumber[i] = qty_phys_reg[q] + reg_offset[i];
  1432.     if (qty_scratch_rtx[q])
  1433.       {
  1434.         PUT_CODE (qty_scratch_rtx[q], REG);
  1435.         REGNO (qty_scratch_rtx[q]) = qty_phys_reg[q];
  1436.  
  1437.         for (i = HARD_REGNO_NREGS (qty_phys_reg[q],
  1438.                        GET_MODE (qty_scratch_rtx[q])) - 1;
  1439.          i >= 0; i--)
  1440.           regs_ever_live[qty_phys_reg[q] + i] = 1;
  1441.  
  1442.         /* Must clear the USED field, because it will have been set by
  1443.            copy_rtx_if_shared, but the leaf_register code expects that
  1444.            it is zero in all REG rtx.  copy_rtx_if_shared does not set the
  1445.            used bit for REGs, but does for SCRATCHes.  */
  1446.         qty_scratch_rtx[q]->used = 0;
  1447.       }
  1448.       }
  1449. }
  1450.  
  1451. /* Compare two quantities' priority for getting real registers.
  1452.    We give shorter-lived quantities higher priority.
  1453.    Quantities with more references are also preferred, as are quantities that
  1454.    require multiple registers.  This is the identical prioritization as
  1455.    done by global-alloc.
  1456.  
  1457.    We used to give preference to registers with *longer* lives, but using
  1458.    the same algorithm in both local- and global-alloc can speed up execution
  1459.    of some programs by as much as a factor of three!  */
  1460.  
  1461. static int
  1462. qty_compare (q1, q2)
  1463.      int q1, q2;
  1464. {
  1465.   /* Note that the quotient will never be bigger than
  1466.      the value of floor_log2 times the maximum number of
  1467.      times a register can occur in one insn (surely less than 100).
  1468.      Multiplying this by 10000 can't overflow.  */
  1469.   register int pri1
  1470.     = (((double) (floor_log2 (qty_n_refs[q1]) * qty_n_refs[q1])
  1471.     / ((qty_death[q1] - qty_birth[q1]) * qty_size[q1]))
  1472.        * 10000);
  1473.   register int pri2
  1474.     = (((double) (floor_log2 (qty_n_refs[q2]) * qty_n_refs[q2])
  1475.     / ((qty_death[q2] - qty_birth[q2]) * qty_size[q2]))
  1476.        * 10000);
  1477.   return pri2 - pri1;
  1478. }
  1479.  
  1480. static int
  1481. qty_compare_1 (q1, q2)
  1482.      short *q1, *q2;
  1483. {
  1484.   register int tem;
  1485.  
  1486.   /* Note that the quotient will never be bigger than
  1487.      the value of floor_log2 times the maximum number of
  1488.      times a register can occur in one insn (surely less than 100).
  1489.      Multiplying this by 10000 can't overflow.  */
  1490.   register int pri1
  1491.     = (((double) (floor_log2 (qty_n_refs[*q1]) * qty_n_refs[*q1])
  1492.     / ((qty_death[*q1] - qty_birth[*q1]) * qty_size[*q1]))
  1493.        * 10000);
  1494.   register int pri2
  1495.     = (((double) (floor_log2 (qty_n_refs[*q2]) * qty_n_refs[*q2])
  1496.     / ((qty_death[*q2] - qty_birth[*q2]) * qty_size[*q2]))
  1497.        * 10000);
  1498.  
  1499.   tem = pri2 - pri1;
  1500.   if (tem != 0) return tem;
  1501.   /* If qtys are equally good, sort by qty number,
  1502.      so that the results of qsort leave nothing to chance.  */
  1503.   return *q1 - *q2;
  1504. }
  1505.  
  1506. /* Attempt to combine the two registers (rtx's) USEDREG and SETREG.
  1507.    Returns 1 if have done so, or 0 if cannot.
  1508.  
  1509.    Combining registers means marking them as having the same quantity
  1510.    and adjusting the offsets within the quantity if either of
  1511.    them is a SUBREG).
  1512.  
  1513.    We don't actually combine a hard reg with a pseudo; instead
  1514.    we just record the hard reg as the suggestion for the pseudo's quantity.
  1515.    If we really combined them, we could lose if the pseudo lives
  1516.    across an insn that clobbers the hard reg (eg, movstr).
  1517.  
  1518.    ALREADY_DEAD is non-zero if USEDREG is known to be dead even though
  1519.    there is no REG_DEAD note on INSN.  This occurs during the processing
  1520.    of REG_NO_CONFLICT blocks.
  1521.  
  1522.    MAY_SAVE_COPYCOPY is non-zero if this insn is simply copying USEDREG to
  1523.    SETREG or if the input and output must share a register.
  1524.    In that case, we record a hard reg suggestion in QTY_PHYS_COPY_SUGG.
  1525.    
  1526.    There are elaborate checks for the validity of combining.  */
  1527.  
  1528.    
  1529. static int
  1530. combine_regs (usedreg, setreg, may_save_copy, insn_number, insn, already_dead)
  1531.      rtx usedreg, setreg;
  1532.      int may_save_copy;
  1533.      int insn_number;
  1534.      rtx insn;
  1535.      int already_dead;
  1536. {
  1537.   register int ureg, sreg;
  1538.   register int offset = 0;
  1539.   int usize, ssize;
  1540.   register int sqty;
  1541.  
  1542.   /* Determine the numbers and sizes of registers being used.  If a subreg
  1543.      is present that does not change the entire register, don't consider
  1544.      this a copy insn.  */
  1545.  
  1546.   while (GET_CODE (usedreg) == SUBREG)
  1547.     {
  1548.       if (GET_MODE_SIZE (GET_MODE (SUBREG_REG (usedreg))) > UNITS_PER_WORD)
  1549.     may_save_copy = 0;
  1550.       offset += SUBREG_WORD (usedreg);
  1551.       usedreg = SUBREG_REG (usedreg);
  1552.     }
  1553.   if (GET_CODE (usedreg) != REG)
  1554.     return 0;
  1555.   ureg = REGNO (usedreg);
  1556.   usize = REG_SIZE (usedreg);
  1557.  
  1558.   while (GET_CODE (setreg) == SUBREG)
  1559.     {
  1560.       if (GET_MODE_SIZE (GET_MODE (SUBREG_REG (setreg))) > UNITS_PER_WORD)
  1561.     may_save_copy = 0;
  1562.       offset -= SUBREG_WORD (setreg);
  1563.       setreg = SUBREG_REG (setreg);
  1564.     }
  1565.   if (GET_CODE (setreg) != REG)
  1566.     return 0;
  1567.   sreg = REGNO (setreg);
  1568.   ssize = REG_SIZE (setreg);
  1569.  
  1570.   /* If UREG is a pseudo-register that hasn't already been assigned a
  1571.      quantity number, it means that it is not local to this block or dies
  1572.      more than once.  In either event, we can't do anything with it.  */
  1573.   if ((ureg >= FIRST_PSEUDO_REGISTER && reg_qty[ureg] < 0)
  1574.       /* Do not combine registers unless one fits within the other.  */
  1575.       || (offset > 0 && usize + offset > ssize)
  1576.       || (offset < 0 && usize + offset < ssize)
  1577.       /* Do not combine with a smaller already-assigned object
  1578.      if that smaller object is already combined with something bigger. */
  1579.       || (ssize > usize && ureg >= FIRST_PSEUDO_REGISTER
  1580.       && usize < qty_size[reg_qty[ureg]])
  1581.       /* Can't combine if SREG is not a register we can allocate.  */
  1582.       || (sreg >= FIRST_PSEUDO_REGISTER && reg_qty[sreg] == -1)
  1583.       /* Don't combine with a pseudo mentioned in a REG_NO_CONFLICT note.
  1584.      These have already been taken care of.  This probably wouldn't
  1585.      combine anyway, but don't take any chances.  */
  1586.       || (ureg >= FIRST_PSEUDO_REGISTER
  1587.       && find_reg_note (insn, REG_NO_CONFLICT, usedreg))
  1588.       /* Don't tie something to itself.  In most cases it would make no
  1589.      difference, but it would screw up if the reg being tied to itself
  1590.      also dies in this insn.  */
  1591.       || ureg == sreg
  1592.       /* Don't try to connect two different hardware registers.  */
  1593.       || (ureg < FIRST_PSEUDO_REGISTER && sreg < FIRST_PSEUDO_REGISTER)
  1594.       /* Don't connect two different machine modes if they have different
  1595.      implications as to which registers may be used.  */
  1596.       || !MODES_TIEABLE_P (GET_MODE (usedreg), GET_MODE (setreg)))
  1597.     return 0;
  1598.  
  1599.   /* Now, if UREG is a hard reg and SREG is a pseudo, record the hard reg in
  1600.      qty_phys_sugg for the pseudo instead of tying them.
  1601.  
  1602.      Return "failure" so that the lifespan of UREG is terminated here;
  1603.      that way the two lifespans will be disjoint and nothing will prevent
  1604.      the pseudo reg from being given this hard reg.  */
  1605.  
  1606.   if (ureg < FIRST_PSEUDO_REGISTER)
  1607.     {
  1608.       /* Allocate a quantity number so we have a place to put our
  1609.      suggestions.  */
  1610.       if (reg_qty[sreg] == -2)
  1611.     reg_is_born (setreg, 2 * insn_number);
  1612.  
  1613.       if (reg_qty[sreg] >= 0)
  1614.     {
  1615.       if (may_save_copy)
  1616.         {
  1617.           SET_HARD_REG_BIT (qty_phys_copy_sugg[reg_qty[sreg]], ureg);
  1618.           qty_phys_has_copy_sugg[reg_qty[sreg]] = 1;
  1619.         }
  1620.       else
  1621.         {
  1622.           SET_HARD_REG_BIT (qty_phys_sugg[reg_qty[sreg]], ureg);
  1623.           qty_phys_has_sugg[reg_qty[sreg]] = 1;
  1624.         }
  1625.     }
  1626.       return 0;
  1627.     }
  1628.  
  1629.   /* Similarly for SREG a hard register and UREG a pseudo register.  */
  1630.  
  1631.   if (sreg < FIRST_PSEUDO_REGISTER)
  1632.     {
  1633.       if (may_save_copy)
  1634.     {
  1635.       SET_HARD_REG_BIT (qty_phys_copy_sugg[reg_qty[ureg]], sreg);
  1636.       qty_phys_has_copy_sugg[reg_qty[ureg]] = 1;
  1637.     }
  1638.       else
  1639.     {
  1640.       SET_HARD_REG_BIT (qty_phys_sugg[reg_qty[ureg]], sreg);
  1641.       qty_phys_has_sugg[reg_qty[ureg]] = 1;
  1642.     }
  1643.       return 0;
  1644.     }
  1645.  
  1646.   /* At this point we know that SREG and UREG are both pseudos.
  1647.      Do nothing if SREG already has a quantity or is a register that we
  1648.      don't allocate.  */
  1649.   if (reg_qty[sreg] >= -1
  1650.       /* If we are not going to let any regs live across calls,
  1651.      don't tie a call-crossing reg to a non-call-crossing reg.  */
  1652.       || (current_function_has_nonlocal_label
  1653.       && ((reg_n_calls_crossed[ureg] > 0)
  1654.           != (reg_n_calls_crossed[sreg] > 0))))
  1655.     return 0;
  1656.  
  1657.   /* We don't already know about SREG, so tie it to UREG
  1658.      if this is the last use of UREG, provided the classes they want
  1659.      are compatible.  */
  1660.  
  1661.   if ((already_dead || find_regno_note (insn, REG_DEAD, ureg))
  1662.       && reg_meets_class_p (sreg, qty_min_class[reg_qty[ureg]]))
  1663.     {
  1664.       /* Add SREG to UREG's quantity.  */
  1665.       sqty = reg_qty[ureg];
  1666.       reg_qty[sreg] = sqty;
  1667.       reg_offset[sreg] = reg_offset[ureg] + offset;
  1668.       reg_next_in_qty[sreg] = qty_first_reg[sqty];
  1669.       qty_first_reg[sqty] = sreg;
  1670.  
  1671.       /* If SREG's reg class is smaller, set qty_min_class[SQTY].  */
  1672.       update_qty_class (sqty, sreg);
  1673.  
  1674.       /* Update info about quantity SQTY.  */
  1675.       qty_n_calls_crossed[sqty] += reg_n_calls_crossed[sreg];
  1676.       qty_n_refs[sqty] += reg_n_refs[sreg];
  1677.       if (usize < ssize)
  1678.     {
  1679.       register int i;
  1680.  
  1681.       for (i = qty_first_reg[sqty]; i >= 0; i = reg_next_in_qty[i])
  1682.         reg_offset[i] -= offset;
  1683.  
  1684.       qty_size[sqty] = ssize;
  1685.       qty_mode[sqty] = GET_MODE (setreg);
  1686.     }
  1687.     }
  1688.   else
  1689.     return 0;
  1690.  
  1691.   return 1;
  1692. }
  1693.  
  1694. /* Return 1 if the preferred class of REG allows it to be tied
  1695.    to a quantity or register whose class is CLASS.
  1696.    True if REG's reg class either contains or is contained in CLASS.  */
  1697.  
  1698. static int
  1699. reg_meets_class_p (reg, class)
  1700.      int reg;
  1701.      enum reg_class class;
  1702. {
  1703.   register enum reg_class rclass = reg_preferred_class (reg);
  1704.   return (reg_class_subset_p (rclass, class)
  1705.       || reg_class_subset_p (class, rclass));
  1706. }
  1707.  
  1708. /* Return 1 if the two specified classes have registers in common.
  1709.    If CALL_SAVED, then consider only call-saved registers.  */
  1710.  
  1711. static int
  1712. reg_classes_overlap_p (c1, c2, call_saved)
  1713.      register enum reg_class c1;
  1714.      register enum reg_class c2;
  1715.      int call_saved;
  1716. {
  1717.   HARD_REG_SET c;
  1718.   int i;
  1719.  
  1720.   COPY_HARD_REG_SET (c, reg_class_contents[(int) c1]);
  1721.   AND_HARD_REG_SET (c, reg_class_contents[(int) c2]);
  1722.  
  1723.   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
  1724.     if (TEST_HARD_REG_BIT (c, i)
  1725.     && (! call_saved || ! call_used_regs[i]))
  1726.       return 1;
  1727.  
  1728.   return 0;
  1729. }
  1730.  
  1731. /* Update the class of QTY assuming that REG is being tied to it.  */
  1732.  
  1733. static void
  1734. update_qty_class (qty, reg)
  1735.      int qty;
  1736.      int reg;
  1737. {
  1738.   enum reg_class rclass = reg_preferred_class (reg);
  1739.   if (reg_class_subset_p (rclass, qty_min_class[qty]))
  1740.     qty_min_class[qty] = rclass;
  1741.  
  1742.   rclass = reg_alternate_class (reg);
  1743.   if (reg_class_subset_p (rclass, qty_alternate_class[qty]))
  1744.     qty_alternate_class[qty] = rclass;
  1745. }
  1746.  
  1747. /* Handle something which alters the value of an rtx REG.
  1748.  
  1749.    REG is whatever is set or clobbered.  SETTER is the rtx that
  1750.    is modifying the register.
  1751.  
  1752.    If it is not really a register, we do nothing.
  1753.    The file-global variables `this_insn' and `this_insn_number'
  1754.    carry info from `block_alloc'.  */
  1755.  
  1756. static void
  1757. reg_is_set (reg, setter)
  1758.      rtx reg;
  1759.      rtx setter;
  1760. {
  1761.   /* Note that note_stores will only pass us a SUBREG if it is a SUBREG of
  1762.      a hard register.  These may actually not exist any more.  */
  1763.  
  1764.   if (GET_CODE (reg) != SUBREG
  1765.       && GET_CODE (reg) != REG)
  1766.     return;
  1767.  
  1768.   /* Mark this register as being born.  If it is used in a CLOBBER, mark
  1769.      it as being born halfway between the previous insn and this insn so that
  1770.      it conflicts with our inputs but not the outputs of the previous insn.  */
  1771.  
  1772.   reg_is_born (reg, 2 * this_insn_number - (GET_CODE (setter) == CLOBBER));
  1773. }
  1774.  
  1775. /* Handle beginning of the life of register REG.
  1776.    BIRTH is the index at which this is happening.  */
  1777.  
  1778. static void
  1779. reg_is_born (reg, birth)
  1780.      rtx reg;
  1781.      int birth;
  1782. {
  1783.   register int regno;
  1784.      
  1785.   if (GET_CODE (reg) == SUBREG)
  1786.     regno = REGNO (SUBREG_REG (reg)) + SUBREG_WORD (reg);
  1787.   else
  1788.     regno = REGNO (reg);
  1789.  
  1790.   if (regno < FIRST_PSEUDO_REGISTER)
  1791.     {
  1792.       mark_life (regno, GET_MODE (reg), 1);
  1793.  
  1794.       /* If the register was to have been born earlier that the present
  1795.      insn, mark it as live where it is actually born.  */
  1796.       if (birth < 2 * this_insn_number)
  1797.     post_mark_life (regno, GET_MODE (reg), 1, birth, 2 * this_insn_number);
  1798.     }
  1799.   else
  1800.     {
  1801.       if (reg_qty[regno] == -2)
  1802.     alloc_qty (regno, GET_MODE (reg), PSEUDO_REGNO_SIZE (regno), birth);
  1803.  
  1804.       /* If this register has a quantity number, show that it isn't dead.  */
  1805.       if (reg_qty[regno] >= 0)
  1806.     qty_death[reg_qty[regno]] = -1;
  1807.     }
  1808. }
  1809.  
  1810. /* Record the death of REG in the current insn.  If OUTPUT_P is non-zero,
  1811.    REG is an output that is dying (i.e., it is never used), otherwise it
  1812.    is an input (the normal case).
  1813.    If OUTPUT_P is 1, then we extend the life past the end of this insn.  */
  1814.  
  1815. static void
  1816. wipe_dead_reg (reg, output_p)
  1817.      register rtx reg;
  1818.      int output_p;
  1819. {
  1820.   register int regno = REGNO (reg);
  1821.  
  1822.   /* If this insn has multiple results,
  1823.      and the dead reg is used in one of the results,
  1824.      extend its life to after this insn,
  1825.      so it won't get allocated together with any other result of this insn.  */
  1826.   if (GET_CODE (PATTERN (this_insn)) == PARALLEL
  1827.       && !single_set (this_insn))
  1828.     {
  1829.       int i;
  1830.       for (i = XVECLEN (PATTERN (this_insn), 0) - 1; i >= 0; i--)
  1831.     {
  1832.       rtx set = XVECEXP (PATTERN (this_insn), 0, i);
  1833.       if (GET_CODE (set) == SET
  1834.           && GET_CODE (SET_DEST (set)) != REG
  1835.           && !rtx_equal_p (reg, SET_DEST (set))
  1836.           && reg_overlap_mentioned_p (reg, SET_DEST (set)))
  1837.         output_p = 1;
  1838.     }
  1839.     }
  1840.  
  1841.   if (regno < FIRST_PSEUDO_REGISTER)
  1842.     {
  1843.       mark_life (regno, GET_MODE (reg), 0);
  1844.  
  1845.       /* If a hard register is dying as an output, mark it as in use at
  1846.      the beginning of this insn (the above statement would cause this
  1847.      not to happen).  */
  1848.       if (output_p)
  1849.     post_mark_life (regno, GET_MODE (reg), 1,
  1850.             2 * this_insn_number, 2 * this_insn_number+ 1);
  1851.     }
  1852.  
  1853.   else if (reg_qty[regno] >= 0)
  1854.     qty_death[reg_qty[regno]] = 2 * this_insn_number + output_p;
  1855. }
  1856.  
  1857. /* Find a block of SIZE words of hard regs in reg_class CLASS
  1858.    that can hold something of machine-mode MODE
  1859.      (but actually we test only the first of the block for holding MODE)
  1860.    and still free between insn BORN_INDEX and insn DEAD_INDEX,
  1861.    and return the number of the first of them.
  1862.    Return -1 if such a block cannot be found. 
  1863.    If QTY crosses calls, insist on a register preserved by calls,
  1864.    unless ACCEPT_CALL_CLOBBERED is nonzero.
  1865.  
  1866.    If JUST_TRY_SUGGESTED is non-zero, only try to see if the suggested
  1867.    register is available.  If not, return -1.  */
  1868.  
  1869. static int
  1870. find_free_reg (class, mode, qty, accept_call_clobbered, just_try_suggested,
  1871.            born_index, dead_index)
  1872.      enum reg_class class;
  1873.      enum machine_mode mode;
  1874.      int accept_call_clobbered;
  1875.      int just_try_suggested;
  1876.      int qty;
  1877.      int born_index, dead_index;
  1878. {
  1879.   register int i, ins;
  1880. #ifdef HARD_REG_SET
  1881.   register        /* Declare it register if it's a scalar.  */
  1882. #endif
  1883.     HARD_REG_SET used, first_used;
  1884. #ifdef ELIMINABLE_REGS
  1885.   static struct {int from, to; } eliminables[] = ELIMINABLE_REGS;
  1886. #endif
  1887.  
  1888.   /* Validate our parameters.  */
  1889.   if (born_index < 0 || born_index > dead_index)
  1890.     abort ();
  1891.  
  1892.   /* Don't let a pseudo live in a reg across a function call
  1893.      if we might get a nonlocal goto.  */
  1894.   if (current_function_has_nonlocal_label
  1895.       && qty_n_calls_crossed[qty] > 0)
  1896.     return -1;
  1897.  
  1898.   if (accept_call_clobbered)
  1899.     COPY_HARD_REG_SET (used, call_fixed_reg_set);
  1900.   else if (qty_n_calls_crossed[qty] == 0)
  1901.     COPY_HARD_REG_SET (used, fixed_reg_set);
  1902.   else
  1903.     COPY_HARD_REG_SET (used, call_used_reg_set);
  1904.  
  1905.   for (ins = born_index; ins < dead_index; ins++)
  1906.     IOR_HARD_REG_SET (used, regs_live_at[ins]);
  1907.  
  1908.   IOR_COMPL_HARD_REG_SET (used, reg_class_contents[(int) class]);
  1909.  
  1910.   /* Don't use the frame pointer reg in local-alloc even if
  1911.      we may omit the frame pointer, because if we do that and then we
  1912.      need a frame pointer, reload won't know how to move the pseudo
  1913.      to another hard reg.  It can move only regs made by global-alloc.
  1914.  
  1915.      This is true of any register that can be eliminated.  */
  1916. #ifdef ELIMINABLE_REGS
  1917.   for (i = 0; i < sizeof eliminables / sizeof eliminables[0]; i++)
  1918.     SET_HARD_REG_BIT (used, eliminables[i].from);
  1919. #else
  1920.   SET_HARD_REG_BIT (used, FRAME_POINTER_REGNUM);
  1921. #endif
  1922.  
  1923.   /* Normally, the registers that can be used for the first register in
  1924.      a multi-register quantity are the same as those that can be used for
  1925.      subsequent registers.  However, if just trying suggested registers,
  1926.      restrict our consideration to them.  If there are copy-suggested
  1927.      register, try them.  Otherwise, try the arithmetic-suggested
  1928.      registers.  */
  1929.   COPY_HARD_REG_SET (first_used, used);
  1930.  
  1931.   if (just_try_suggested)
  1932.     {
  1933.       if (qty_phys_has_copy_sugg[qty])
  1934.     IOR_COMPL_HARD_REG_SET (first_used, qty_phys_copy_sugg[qty]);
  1935.       else
  1936.     IOR_COMPL_HARD_REG_SET (first_used, qty_phys_sugg[qty]);
  1937.     }
  1938.  
  1939.   /* If all registers are excluded, we can't do anything.  */
  1940.   GO_IF_HARD_REG_SUBSET (reg_class_contents[(int) ALL_REGS], first_used, fail);
  1941.  
  1942.   /* If at least one would be suitable, test each hard reg.  */
  1943.  
  1944.   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
  1945.     {
  1946. #ifdef REG_ALLOC_ORDER
  1947.       int regno = reg_alloc_order[i];
  1948. #else
  1949.       int regno = i;
  1950. #endif
  1951.       if (! TEST_HARD_REG_BIT (first_used, regno)
  1952.       && HARD_REGNO_MODE_OK (regno, mode))
  1953.     {
  1954.       register int j;
  1955.       register int size1 = HARD_REGNO_NREGS (regno, mode);
  1956.       for (j = 1; j < size1 && ! TEST_HARD_REG_BIT (used, regno + j); j++);
  1957.       if (j == size1)
  1958.         {
  1959.           /* Mark that this register is in use between its birth and death
  1960.          insns.  */
  1961.           post_mark_life (regno, mode, 1, born_index, dead_index);
  1962.           return regno;
  1963.         }
  1964. #ifndef REG_ALLOC_ORDER
  1965.       i += j;        /* Skip starting points we know will lose */
  1966. #endif
  1967.     }
  1968.     }
  1969.  
  1970.  fail:
  1971.  
  1972.   /* If we are just trying suggested register, we have just tried copy-
  1973.      suggested registers, and there are arithmetic-suggested registers,
  1974.      try them.  */
  1975.   
  1976.   /* If it would be profitable to allocate a call-clobbered register
  1977.      and save and restore it around calls, do that.  */
  1978.   if (just_try_suggested && qty_phys_has_copy_sugg[qty]
  1979.       && qty_phys_has_sugg[qty])
  1980.     {
  1981.       /* Don't try the copy-suggested regs again.  */
  1982.       qty_phys_has_copy_sugg[qty] = 0;
  1983.       return find_free_reg (class, mode, qty, accept_call_clobbered, 1,
  1984.                 born_index, dead_index);
  1985.     }
  1986.  
  1987.   if (! accept_call_clobbered
  1988.       && flag_caller_saves
  1989.       && ! just_try_suggested
  1990.       && qty_n_calls_crossed[qty] != 0
  1991.       && CALLER_SAVE_PROFITABLE (qty_n_refs[qty], qty_n_calls_crossed[qty]))
  1992.     {
  1993.       i = find_free_reg (class, mode, qty, 1, 0, born_index, dead_index);
  1994.       if (i >= 0)
  1995.     caller_save_needed = 1;
  1996.       return i;
  1997.     }
  1998.   return -1;
  1999. }
  2000.  
  2001. /* Mark that REGNO with machine-mode MODE is live starting from the current
  2002.    insn (if LIFE is non-zero) or dead starting at the current insn (if LIFE
  2003.    is zero).  */
  2004.  
  2005. static void
  2006. mark_life (regno, mode, life)
  2007.      register int regno;
  2008.      enum machine_mode mode;
  2009.      int life;
  2010. {
  2011.   register int j = HARD_REGNO_NREGS (regno, mode);
  2012.   if (life)
  2013.     while (--j >= 0)
  2014.       SET_HARD_REG_BIT (regs_live, regno + j);
  2015.   else
  2016.     while (--j >= 0)
  2017.       CLEAR_HARD_REG_BIT (regs_live, regno + j);
  2018. }
  2019.  
  2020. /* Mark register number REGNO (with machine-mode MODE) as live (if LIFE
  2021.    is non-zero) or dead (if LIFE is zero) from insn number BIRTH (inclusive)
  2022.    to insn number DEATH (exclusive).  */
  2023.  
  2024. static void
  2025. post_mark_life (regno, mode, life, birth, death)
  2026.      register int regno, life, birth;
  2027.      enum machine_mode mode;
  2028.      int death;
  2029. {
  2030.   register int j = HARD_REGNO_NREGS (regno, mode);
  2031. #ifdef HARD_REG_SET
  2032.   register        /* Declare it register if it's a scalar.  */
  2033. #endif
  2034.     HARD_REG_SET this_reg;
  2035.  
  2036.   CLEAR_HARD_REG_SET (this_reg);
  2037.   while (--j >= 0)
  2038.     SET_HARD_REG_BIT (this_reg, regno + j);
  2039.  
  2040.   if (life)
  2041.     while (birth < death)
  2042.       {
  2043.     IOR_HARD_REG_SET (regs_live_at[birth], this_reg);
  2044.     birth++;
  2045.       }
  2046.   else
  2047.     while (birth < death)
  2048.       {
  2049.     AND_COMPL_HARD_REG_SET (regs_live_at[birth], this_reg);
  2050.     birth++;
  2051.       }
  2052. }
  2053.  
  2054. /* INSN is the CLOBBER insn that starts a REG_NO_NOCONFLICT block, R0
  2055.    is the register being clobbered, and R1 is a register being used in
  2056.    the equivalent expression.
  2057.  
  2058.    If R1 dies in the block and has a REG_NO_CONFLICT note on every insn
  2059.    in which it is used, return 1.
  2060.  
  2061.    Otherwise, return 0.  */
  2062.  
  2063. static int
  2064. no_conflict_p (insn, r0, r1)
  2065.      rtx insn, r0, r1;
  2066. {
  2067.   int ok = 0;
  2068.   rtx note = find_reg_note (insn, REG_LIBCALL, NULL_RTX);
  2069.   rtx p, last;
  2070.  
  2071.   /* If R1 is a hard register, return 0 since we handle this case
  2072.      when we scan the insns that actually use it.  */
  2073.  
  2074.   if (note == 0
  2075.       || (GET_CODE (r1) == REG && REGNO (r1) < FIRST_PSEUDO_REGISTER)
  2076.       || (GET_CODE (r1) == SUBREG && GET_CODE (SUBREG_REG (r1)) == REG
  2077.       && REGNO (SUBREG_REG (r1)) < FIRST_PSEUDO_REGISTER))
  2078.     return 0;
  2079.  
  2080.   last = XEXP (note, 0);
  2081.  
  2082.   for (p = NEXT_INSN (insn); p && p != last; p = NEXT_INSN (p))
  2083.     if (GET_RTX_CLASS (GET_CODE (p)) == 'i')
  2084.       {
  2085.     if (find_reg_note (p, REG_DEAD, r1))
  2086.       ok = 1;
  2087.  
  2088.     if (reg_mentioned_p (r1, PATTERN (p))
  2089.         && ! find_reg_note (p, REG_NO_CONFLICT, r1))
  2090.       return 0;
  2091.       }
  2092.       
  2093.   return ok;
  2094. }
  2095.  
  2096. #ifdef REGISTER_CONSTRAINTS
  2097.  
  2098. /* Return 1 if the constraint string P indicates that the a the operand
  2099.    must be equal to operand 0 and that no register is acceptable.  */
  2100.  
  2101. static int
  2102. requires_inout_p (p)
  2103.      char *p;
  2104. {
  2105.   char c;
  2106.   int found_zero = 0;
  2107.  
  2108.   while (c = *p++)
  2109.     switch (c)
  2110.       {
  2111.       case '0':
  2112.     found_zero = 1;
  2113.     break;
  2114.  
  2115.       case '=':  case '+':  case '?':
  2116.       case '#':  case '&':  case '!':
  2117.       case '*':  case '%':  case ',':
  2118.       case '1':  case '2':  case '3':  case '4':
  2119.       case 'm':  case '<':  case '>':  case 'V':  case 'o':
  2120.       case 'E':  case 'F':  case 'G':  case 'H':
  2121.       case 's':  case 'i':  case 'n':
  2122.       case 'I':  case 'J':  case 'K':  case 'L':
  2123.       case 'M':  case 'N':  case 'O':  case 'P':
  2124. #ifdef EXTRA_CONSTRAINT
  2125.       case 'Q':  case 'R':  case 'S':  case 'T':  case 'U':
  2126. #endif
  2127.       case 'X':
  2128.     /* These don't say anything we care about.  */
  2129.     break;
  2130.  
  2131.       case 'p':
  2132.       case 'g': case 'r':
  2133.       default:
  2134.     /* These mean a register is allowed.  Fail if so.  */
  2135.     return 0;
  2136.       }
  2137.  
  2138.   return found_zero;
  2139. }
  2140. #endif /* REGISTER_CONSTRAINTS */
  2141.  
  2142. void
  2143. dump_local_alloc (file)
  2144.      FILE *file;
  2145. {
  2146.   register int i;
  2147.   for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
  2148.     if (reg_renumber[i] != -1)
  2149.       fprintf (file, ";; Register %d in %d.\n", i, reg_renumber[i]);
  2150. }
  2151.